<?php

/**
 * Base_App
 *
 * LICENSE
 *
 * This source file is subject to the new CC-GNU LGPL
 * It is available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1/
 *
 * @category   Base_App
 * @copyright  Copyright (c) 2009 Alexander fanatique* Thomas (me@alexander-thomas.net)
 * @license    http://creativecommons.org/licenses/LGPL/2.1/
 */

/**
 * Set environment
 * 
 * @static
 * @todo Hen & egg problem: We need to read some config infos at some point (cf. below)
 */

class Base_App_Env
{
    const APP_KEY = 'base-app';
    
	/**
	 * undocumented function
	 *
	 * @return string 
	 * @author Alexander Thomas
	 */
    public static function setLoadEnvironment()
    {
        $dirname = dirname(__FILE__);
        $base    = substr($dirname, 0, strrpos($dirname, self::APP_KEY) + strlen(self::APP_KEY.'/')); 
        
        // Set include paths
		$library_path = $base.'App/library/';
        $application_path = $base.'App/application/';
        $include = array(
            $library_path,
            $application_path
        );

        set_include_path(get_include_path().PATH_SEPARATOR.implode(PATH_SEPARATOR, $include));
        
        // Set up Zend Loader (ZF 1.8)
        
        require_once 'Zend/Loader/Autoloader/Resource.php';
        require_once 'Zend/Loader/Autoloader.php';

        /**
         * @todo Set $autoloader->suppressNotFoundWarnings(true) when in non-development environment 
         */
        
        // Autoload vendor libs
        
        $autoloader = Zend_Loader_Autoloader::getInstance();
        
        // Autoload system
        
        $autoloader->registerNamespace('Base_');
        
        // Configure userland
        
        /**
         * @todo User resources need to be configurable via some config file
         */
        
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'  => $base,
            'namespace' => '',
        ));
        
        $resourceLoader->addResourceType('service', 'App/application/Service/', 'Service')
                       ->addResourceType('model', 'App/application/Model/', 'Model')
                       ->addResourceType('exception', 'App/application/Exception/', 'Exception')
                       ->addResourceType('migrations', 'App/application/Migrations/', 'Migrations')
					   ->addResourceType('cli', 'App/application/Cli/', 'Cli');
                        
        Zend_Registry::set('AppPath', $application_path);
    }

	public static final function set_error_handler(){
		function errorHandler($errno, $errstr, $errfile, $errline){
			throw new Base_App_Exception($errstr, $errno, $errfile, $errline);
		}
		set_error_handler("errorHandler", E_ALL | E_STRICT); 
	}
	
    
    /**
     * @todo Should operate on config info
     */
    
    public static function setTimezone()
    {
        date_default_timezone_set('Europe/Berlin');
    }
    
    /**
     * @todo Should operate on config info
     */
    
    public static function setErrorReporting()
    {
        error_reporting(E_ALL | E_STRICT);
    }
    
	/**
	 * Convinience method to get initilization done by just calling one method.
	 *
	 * @return void
	 * @author Alexander Thomas
	 */
    public static function initApp()
    {
        self::setErrorReporting();
        self::setTimezone();
        self::setLoadEnvironment();
		self::set_error_handler();
    }
}

?>